package net.entelijan.cobean.examples.showcase.orderlist;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.core.SelectionMode;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.orderlist.DefaultOrderlistInitializer;
import net.entelijan.cobean.util.DateConverter;
import net.entelijan.scf.IShowcase;

public class SingleSelectionOrderlistShowcase implements
		IShowcase<SingleSelectionOrderlistPanel> {

	private ILitService service = null;

	public SingleSelectionOrderlistShowcase() {
		super();
	}

	@Override
	public void init(final SingleSelectionOrderlistPanel panel) {

		DefaultOrderlistInitializer<LitAuthor> ini = new DefaultOrderlistInitializer<LitAuthor>();
		ini.setColumnDescs(createColumnDescs());

		ini.setCoList(panel.getOrderlistPanel().getList());
		ini.setCoUpButton(panel.getOrderlistPanel().getUpButton());
		ini.setCoDownButton(panel.getOrderlistPanel().getDownButton());
		ini.setSelectionMode(SelectionMode.SINGLE);

		// Initialize the components and receive the component bean.
		// From now on the component bean is your interface to the 'real'
		// components
		final ICobean<List<LitAuthor>> cobean = ini.initialize();

		cobean.setValue(service.loadAllAuthors());

		panel.getShowListButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, ExamplesUtil.listToString(
						cobean.getValue(), 80));
			}

		});
	}

	private List<ColumnDesc> createColumnDescs() {
		List<ColumnDesc> colDescs = new ArrayList<ColumnDesc>();
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("name");
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("birthday");
			colDesc.setConverter(new DateConverter("yyyy"));
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("country");
			colDescs.add(colDesc);
		}
		return colDescs;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
